package cn.schoolwow.util.module.query.resource.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.module.query.resource.flow.walkResource.common.SetUrlFileFlow;

import java.io.File;

public class GetFileNameFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        getFromFile(flowContext);
        getFromJar(flowContext);
    }

    @Override
    public String name() {
        return "获取资源文件名称";
    }

    private void getFromFile(FlowContext flowContext){
        File file = (File) flowContext.getData("file");
        if(null==file){
            return;
        }
        String fileName = file.getName();
        flowContext.putReturnData("fileName" ,fileName);
        flowContext.brokenCurrentFlow("从文件获取");
    }

    private void getFromJar(FlowContext flowContext){
        flowContext.executeFlowList(new SetUrlFileFlow());
        String urlFile = (String) flowContext.checkData("urlFile");

        String fileName = urlFile.substring(urlFile.lastIndexOf("/")+1);
        flowContext.putReturnData("fileName" ,fileName);
        flowContext.brokenCurrentFlow("从jar文件获取");
    }
}
